<template>
  <el-dialog
    :title="!dataForm.restId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="请假时间" prop="restTime">
      <el-date-picker
        v-model="dataForm.time"
        type="datetimerange"
        value-format="yyyy-MM-dd HH:mm:ss"
        range-separator="至"
        start-placeholder="请假开始日期"
        end-placeholder="请假结束日期"
        align="left">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="请假原因" prop="restReason">
      <el-input v-model="dataForm.restReason" placeholder="请假原因"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          restId: 0,
          time: [],
          restReason: ''
        },
        dataRule: {
          time: [
            { required: true, message: '请假时间不能为空', trigger: 'blur' }
          ],
          restReason: [
            { required: true, message: '请假原因不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.restId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.restId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/rest/info/${this.dataForm.restId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.restReason = data.rest.restReason;
                this.dataForm.restTime = [data.rest.startTime, data.rest.endTime];
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/rest/${!this.dataForm.restId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'restId': this.dataForm.restId || undefined,
                'startTime': this.dataForm.time[0],
                'endTime': this.dataForm.time[1],
                'restReason': this.dataForm.restReason
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
              this.dataForm.time = [];
            })
          }
        })
      }
    }
  }
</script>
